package com.ms.dsq_english.wall;

import static android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.ms.dsq_english.R;
import com.ms.dsq_english.adapter.MyFragmentPagerAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WallActivity extends AppCompatActivity {

    @BindView(R.id.wall_vp)
    ViewPager wallVp;
    @BindView(R.id.wall_time)
    TextView wallTime;
    @BindView(R.id.wall_time_y)
    TextView wallTimeY;
    @BindView(R.id.wall_time_z)
    TextView wallTimeZ;

    private List<Fragment> fragmentList;
    private MyFragmentPagerAdapter myFragmentPagerAdapter;
    private  int position =0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wall);
        setWindow();

        ButterKnife.bind(this);
        fragmentList = new ArrayList<>();
        fragmentList.add(0, new PartFFragment());
//        fragmentList.add(1, new SalesFragment());
//        fragmentList.add(2, new StoreFragment());
//        fragmentList.add(3, new AreaFragment());


        myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList);
        wallVp.setAdapter(myFragmentPagerAdapter);
        wallVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                WallActivity.this.position = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        wallVp.setCurrentItem(0);
        new TimeThread().start();
        new TimeThreadd().start();

    }

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    private void setWindow() {
        Window window = getWindow();
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }

    class TimeThread extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;  //消息(一个整型值)
                    mHandler.sendMessage(msg);// 每隔1秒发送一个msg给mHandler


                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    class TimeThreadd extends Thread {
        @Override
        public void run() {
            do {
                try {
                    Thread.sleep(15 * 60 * 1000);
                    Message msg2 = new Message();
                    msg2.what = 2;  //消息(一个整型值)
                    mHandler.sendMessage(msg2);// 每隔1秒发送一个msg给mHandler

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (true);
        }
    }

    //在主线程里面处理消息并更新UI界面
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    long sysTime = System.currentTimeMillis();//获取系统时间
                    CharSequence sysTimeStr = DateFormat.format("HH:mm:ss", sysTime);//时间显示格式
                    wallTime.setText(sysTimeStr); //更新时间
                    CharSequence sysTimey = DateFormat.format("yyyy-MM-dd", sysTime);
                    wallTimeY.setText(sysTimey);
//                    wallTimeZ.setText(getWeek(sysTimey.toString()));
                    wallTimeZ.setText(getEnglishWeek(sysTimey.toString()));
                    break;
                case 2:
                    if (position ==0){
                        ((PartFFragment)fragmentList.get(position)).refresh();
                    }
//                    if (position ==1){
//                        ((SalesFragment)fragmentList.get(position)).refresh();
//                    }
//                    if (position ==2){
//                        ((StoreFragment)fragmentList.get(position)).refresh();
//                    }
//                    if (position ==3){
//                        ((AreaFragment)fragmentList.get(position)).refresh();
//                    }

                    break;
                default:
                    break;

            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(0);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mHandler.removeMessages(0);
    }

    public static String getWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {

            e.printStackTrace();
        }

        int wek=c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "星期日";
        }
        if (wek == 2) {
            Week += "星期一";
        }
        if (wek == 3) {
            Week += "星期二";
        }
        if (wek == 4) {
            Week += "星期三";
        }
        if (wek == 5) {
            Week += "星期四";
        }
        if (wek == 6) {
            Week += "星期五";
        }
        if (wek == 7) {
            Week += "星期六";
        }
        return Week;
    }

    public static String getEnglishWeek(String time) {
        String Week = "";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {
            c.setTime(format.parse(time));
        } catch (ParseException e) {

            e.printStackTrace();
        }

        int wek=c.get(Calendar.DAY_OF_WEEK);

        if (wek == 1) {
            Week += "Sunday";
        }
        if (wek == 2) {
            Week += "Monday";
        }
        if (wek == 3) {
            Week += "Tuesday";
        }
        if (wek == 4) {
            Week += "Wednesday";
        }
        if (wek == 5) {
            Week += "Thursday";
        }
        if (wek == 6) {
            Week += "Friday";
        }
        if (wek == 7) {
            Week += "Saturday";
        }
        return Week;
    }
}
